package org.example.service;

import org.apache.commons.lang3.StringUtils;
import org.example.internalCommon.constant.CommonStatusEnum;
import org.example.internalCommon.constant.IdentityConstants;
import org.example.internalCommon.constant.TokenConstants;
import org.example.internalCommon.dto.ResponseResult;
import org.example.internalCommon.request.VerificationCodeDTO;
import org.example.internalCommon.util.JwtUtils;
import org.example.internalCommon.response.NumberCodeResponse;
import org.example.internalCommon.response.TokenResponse;
import org.example.internalCommon.util.RedisprefixUtils;
import org.example.remote.ServicePassengerUserClient;
import org.example.remote.ServiceVefificationcodeClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class VerificationCodeService {


    @Autowired
    private ServiceVefificationcodeClient serviceVefificationcodeClient;

    @Autowired
    private ServicePassengerUserClient servicePassengerUserClient;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 生成验证码
     *
     * @param passengerPhone
     * @return
     */
    public ResponseResult generatorCode(String passengerPhone){
        // 调用验证码服务，获取验证码
        ResponseResult<NumberCodeResponse> numberCodeResponse = serviceVefificationcodeClient.getNumberCode(6);
        String numberCode = numberCodeResponse.getData().getNumberCode();
        System.out.println("-------------------------------------------" + numberCode);
        // 存入redis
        //k,v,过期时间
        String key = RedisprefixUtils.gemeratorKeyByPhone(passengerPhone, IdentityConstants.PASSENGER_IDENTITY);
        stringRedisTemplate.opsForValue().set(key, numberCode, 2 , TimeUnit.MINUTES);
        // 后期通过短信服务商，将对应的验证码发送到手机上，阿里短信服务。。。。
        return ResponseResult.success();
    }

    /**
     *  校验验证码
     * @param verificationCodeDTO
     * @return
     */
    public ResponseResult checkCode(VerificationCodeDTO verificationCodeDTO) {
        String passengerPhone = verificationCodeDTO.getPassengerPhone();
        // 根据手机号，去redis拿到验证码
        String redisVerificationCode = stringRedisTemplate.opsForValue().get(RedisprefixUtils.gemeratorKeyByPhone(passengerPhone, IdentityConstants.PASSENGER_IDENTITY));
        // 校验验证码
        if (StringUtils.isBlank(redisVerificationCode) || !verificationCodeDTO.getVerificationCode().equals(redisVerificationCode)) {
            return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_ERROR.getCode()
                    , CommonStatusEnum.VERIFICATION_CODE_ERROR.getValue());
        }
        //todo 判断原来是否有用户并进行对应处理
        servicePassengerUserClient.loginOrReg(verificationCodeDTO);
        // 颁发令牌
        String accessToken = JwtUtils.generatorToken(passengerPhone, IdentityConstants.PASSENGER_IDENTITY, TokenConstants.ACCESS_TOKEN_TYPE);
        String refreshToken = JwtUtils.generatorToken(passengerPhone, IdentityConstants.PASSENGER_IDENTITY, TokenConstants.REFRESH_TOKEN_TYPE);

        // token存到redis中
        String accessTokenKey = RedisprefixUtils.generatorTokenKey(passengerPhone, IdentityConstants.PASSENGER_IDENTITY, TokenConstants.ACCESS_TOKEN_TYPE);
        String refreshTokenKey = RedisprefixUtils.generatorTokenKey(passengerPhone, IdentityConstants.PASSENGER_IDENTITY, TokenConstants.REFRESH_TOKEN_TYPE);

        stringRedisTemplate.opsForValue().set(accessTokenKey, accessToken, 5, TimeUnit.SECONDS);
        stringRedisTemplate.opsForValue().set(refreshTokenKey, refreshToken, 200, TimeUnit.SECONDS);

        // 响应
        TokenResponse tokenResponse = new TokenResponse();
        tokenResponse.setAccessToken(accessToken);
        tokenResponse.setRefreshToken(refreshToken);

        return ResponseResult.success(tokenResponse);
    }



}
